//
//  MXFindBlockTypeElevenTableViewCell.m
//  MXの衣柜
//
//  Created by 马霄 on 15/8/14.
//  Copyright (c) 2015年 霄 马. All rights reserved.
//

#import "MXFindBlockTypeElevenTableViewCell.h"
#import "MXFindBlockTypeElevenCollectionViewCell.h"
#import "MXNewViewItemDetailVC.h"
#import "MXFindWebViewVC.h"
#import "MXFindItemDetailWebViewVC.h"
@interface MXFindBlockTypeElevenTableViewCell ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

@end
@implementation MXFindBlockTypeElevenTableViewCell
- (void)dealloc
{
    [_naviEleven release];
    [_model release];
    [_collectionView release];
    [super dealloc];
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self creatCollectionView];
    }
    return self;
}
-(void)layoutSubviews
{
    [super layoutSubviews];
    CGFloat WID = self.contentView.frame.size.width ;
    
    CGFloat collectionViewX = 0;
    CGFloat collectionViewY = 0;
    CGFloat collectionViewW = WID;
    CGFloat collectionViewH = HEIGHTPRO * 660;
    self.collectionView.frame = CGRectMake(collectionViewX, collectionViewY, collectionViewW, collectionViewH);
    
}

#pragma mark - 创建集视图
- (void)creatCollectionView
{
    UICollectionViewFlowLayout *flowLayout = [[[UICollectionViewFlowLayout alloc] init] autorelease];
#warning 需要使用协议方法来确定每个Item的尺寸
    flowLayout.minimumLineSpacing = 10;
    flowLayout.minimumInteritemSpacing = 10;
    flowLayout.sectionInset = UIEdgeInsetsMake(0, HEIGHTPRO * 10, 0, HEIGHTPRO * 10);
    
    CGFloat wid = self.frame.size.width;
    CGFloat height = self.frame.size.height;
    
    
    self.collectionView = [[[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, wid, height  - HEIGHTPRO * 49) collectionViewLayout:flowLayout] autorelease];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:self.collectionView];
    
#warning 需自定义collectionViewcell
    [self.collectionView registerClass:[MXFindBlockTypeElevenCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    
}
#pragma mark - CollectionView 协议实现部分
/************************* CollectionView Delegate **************************/
//Collection中的组别个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 3;
}
//每个组别中元件的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    } else {
        return 2;
    }
    
    
//    return 5;
}
//重用机制
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MXFindBlockTypeElevenCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor magentaColor];
    cell.layer.cornerRadius = 8;
    cell.layer.masksToBounds = YES;
    cell.model = self.model;
    if (indexPath.section == 0) {
        [cell getInfo:self.model rowNum:0];

    } else if (indexPath.section == 1){
        if (indexPath.row == 0) {
            [cell getInfo:self.model rowNum:1];
        } else {
            [cell getInfo:self.model rowNum:2];
        }
    } else if (indexPath.section == 2){
        if (indexPath.row == 0) {
            [cell getInfo:self.model rowNum:3];
        } else {
            [cell getInfo:self.model rowNum:4];
        }

    }
    
//    [cell getInfo:self.model rowNum:indexPath.row];
    return cell;
}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
//    if (indexPath.row == 0) {
//        return CGSizeMake(300, 300);
//    } else {
//        return CGSizeMake(150, 150);
//    }
    if (indexPath.section == 0) {
        return CGSizeMake( WIDTHPRO * 300, HEIGHTPRO * 300);
    } else {
        return CGSizeMake(WIDTHPRO * 150, HEIGHTPRO * 150);
    }
    
    
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    if ( section == 0) {
        return UIEdgeInsetsMake( WIDTHPRO * 10, HEIGHTPRO * 10, WIDTHPRO * 10, HEIGHTPRO * 10);
    }
    else
    {
        return UIEdgeInsetsMake( WIDTHPRO * 10, HEIGHTPRO * 20, WIDTHPRO * 10, HEIGHTPRO * 20);
    }
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    MXFindItemDetailWebViewVC *ItemVC = [[[MXFindItemDetailWebViewVC alloc] init] autorelease];
    
    NSMutableArray *arr = [NSMutableArray array];
    for (NSDictionary *dic in self.model.product) {
        [arr addObject:[dic objectForKey:@"url"]];
    }
    
    ItemVC.requestString = arr[indexPath.row];
    
    ItemVC.TitleString = @"商品详情";

    [self.naviEleven pushViewController:ItemVC animated:YES];
}

- (void)setModel:(MXFindDetailModel *)model
{
    if (_model != model) {
        [_model release];
        _model = [model retain];
    }
    
}


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
